package util;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import bean.XmlBean;

public class XmlToBean {
public static Map<String,XmlBean>  xmlToBean(String xmlPath) throws Exception{
	Map<String,XmlBean> pmap=new HashMap<String,XmlBean>();
	SAXBuilder builder=new SAXBuilder();
	Document document=builder.build(new File(xmlPath));
	Element root=document.getRootElement();
	Element actionform=root.getChild("formbeans");
	Element actionroot=root.getChild("action-mapping");
	List<Element> forms=actionform.getChildren();
	List<Element> actions=actionroot.getChildren();
	for(Element e:actions){
		String path=e.getAttributeValue("path");
		String name=e.getAttributeValue("name");
		XmlBean xmlBean=new XmlBean();
		xmlBean.setPath(path);
		xmlBean.setName(name);
		xmlBean.setActionClass(e.getAttributeValue("class"));
		List<Element> forwards=e.getChildren();
		Map<String,String> forwardsMap=new HashMap<String,String>();
		for(Element e1:forwards){
			forwardsMap.put(e1.getAttributeValue("name"),e1.getAttributeValue("value"));	
        }
		xmlBean.setActionForward(forwardsMap);
		for(Element e2:forms){
			if(e2.getAttributeValue("name").equals(name)){
				xmlBean.setFormClass(e2.getAttributeValue("class"));
				break;
			}
			
		}
		pmap.put(path, xmlBean);
}
	return  pmap;
}

//public static void main(String[] args) throws Exception {
//	Map<String,XmlBean> a=XmlToBean.xmlToBean("src\\config.xml");
//	for(String s:a.keySet()){
//		System.out.println(s);
//		XmlBean xb=a.get(s);
//		System.out.println(xb);
//	}
//	
//	
//}
}